function idealize(DFILE, ctl)
% idealize the current traces and replace existing ones with idealized ones.
%
global CURRENT
if(~strcmp(fieldnames(DFILE), 'stim'))
    return;
end;
if(~strcmp(DFILE.stim, 'steps'))
   return;
end;
fn = fieldnames(DFILE);
if(strmatch(fn, 'seqpar'))
   if(findstr('duration', DFILE.seqpar) > 0)
   return;
end;
end;

nr = length(DFILE.record);
tbase = make_time (DFILE);
if(exist('DFILE.channels') == 0 & DFILE.mode < 5)
   DFILE.channels = DFILE.nr_channel*ones(size(DFILE.rate));
end;

if(size(DFILE.rate, 2) ~= size(DFILE.channels, 2))
   DFILE.channels = DFILE.channels';
end;
R = (DFILE.rate(1:nr) .* DFILE.channels(1:nr)) / 1000;

for i = 1:nr
   t1 = floor(number_arg(ctl.durho)/R(i));
   t2 = floor(number_arg(ctl.durs1)/R(i)) + t1 - 1;
   t3 = floor(number_arg(ctl.durs2)/R(i)) + t2 - 1;
   t4 = floor(number_arg(ctl.durs3)/R(i)) + t3 - 1;
   s = size(CURRENT);
   if(t1 > s | t2 > s | t3 > s | t4 > s | t1 < 1 | t2 < 1 | t3 < 1 | t4 < 1)
      QueMessage('Idealize: Bad settings for duration', 1);
      return;
   end;
   CURRENT(i,1:t1) = mean(CURRENT(i,1:t1));
   if(t2 > t1)
      CURRENT(i,t1+1:t2) = mean(CURRENT(i,t1+1:t2));
   end;
   
   if(t3 > t2)
      CURRENT(i,t2+1:t3) = mean(CURRENT(i, t2+1:t3));
   end;
   
   if(t4 > t3)
      CURRENT(i,t3+1:t4) = mean(CURRENT(i, t3+1:t4));
   end;
   
   if(t4 < size(CURRENT,2))
      CURRENT(i,t4+1:end) = mean(CURRENT(i, t4+1:end));
   end;
end;
return

	 